<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="归属部门" prop="deptId">
        <treeselect v-model="queryParams.deptId" :options="deptOptions" :disable-branch-nodes="false" :show-count="true"
                    size="small"
                    :default-expand-level="1"
                    style="width: 240px"
                    placeholder="请选择归属部门"/>
      </el-form-item>
      <el-form-item label="名称" prop="shiftName">
        <el-input
          v-model="queryParams.shiftName"
          placeholder="请输入名称"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="工时类型" prop="workerType">
        <el-select v-model="queryParams.workerType" placeholder="请选择工时类型" clearable size="small">
          <el-option key="1" label="全职工" value="1" />
          <el-option key="2" label="小时工" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="打卡方式" prop="clockInType">
        <el-select v-model="queryParams.clockInType" placeholder="请选择打卡方式" clearable size="small">
          <el-option key="1" label="多次卡" value="1" />
          <el-option key="2" label="一次卡" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:schedefine:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:schedefine:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:schedefine:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:schedefine:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="schedefineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="" align="center" prop="id" />
<!--      <el-table-column label="组织ID" align="center" prop="deptId" />-->
      <el-table-column label="名称" align="center" prop="shiftName"  />
      <el-table-column label="描述" align="center" prop="comment"  />
      <el-table-column label="工时类型" align="center" prop="workerType"   />
      <el-table-column label="班次类别" align="center" prop="scheduleType"   />
      <el-table-column label="打卡方式" align="center" prop="clockInType"  />
      <el-table-column label="第一次打卡开始时间" align="center" prop="scheduleStart1" width="180">
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.scheduleStart1, '{h}:{m}') }}</span>-->
<!--        </template>-->
      </el-table-column>

      <el-table-column label="是否跨天" align="center" prop="scheduleStart1NextDay"  />
      <el-table-column label="第一次打卡结束时间" align="center" prop="scheduleEnd1" width="180">
      </el-table-column>
      <el-table-column label="是否跨天" align="center" prop="scheduleEnd1NextDay" :formatter="trueFalseFormat" />
      <el-table-column label="第二次打卡开始时间" align="center" prop="scheduleStart2" width="180">
      </el-table-column>
      <el-table-column label="是否跨天" align="center" prop="scheduleStart2NextDay" :formatter="trueFalseFormat" />
      <el-table-column label="第二次打卡结束时间" align="center" prop="scheduleEnd2" width="180">
      </el-table-column>
      <el-table-column label="是否跨天" align="center" prop="scheduleEnd2NextDay" :formatter="trueFalseFormat" />
      <el-table-column label="第三次打卡开始时间" align="center" prop="scheduleStart3" width="180">

      </el-table-column>
      <el-table-column label="是否跨天" align="center" prop="scheduleStart3NextDay" :formatter="trueFalseFormat" />
      <el-table-column label="第三次打卡结束时间" align="center" prop="scheduleEnd3" width="180">

      </el-table-column>
      <el-table-column label="是否跨天" align="center" prop="scheduleEnd3NextDay" :formatter="trueFalseFormat" />
      <el-table-column label="第四次打卡开始时间" align="center" prop="scheduleStart4" width="180">

      </el-table-column>
      <el-table-column label="是否跨天" align="center" prop="scheduleStart4NextDay" :formatter="trueFalseFormat" />
      <el-table-column label="第四次打卡结束时间" align="center" prop="scheduleEnd4" width="180">

      </el-table-column>
      <el-table-column label="是否跨天" align="center" prop="scheduleEnd4NextDay" :formatter="trueFalseFormat" />
      <el-table-column label="是否启用" align="center" prop="enable" :formatter="trueFalseFormat" />
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:schedefine:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:schedefine:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改班次定义对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="归属部门" prop="deptId" size="small" style="width: 200px" >
          <treeselect v-model="form.deptId" :options="deptOptions" :disable-branch-nodes="false" :show-count="true"
                      size="small"
                      :default-expand-level="1"
                      style="width: 200px"
                      placeholder="请选择归属部门"/>
        </el-form-item>
        <el-form-item label="名称" prop="shiftName">
          <el-input v-model="form.shiftName" size="small" style="width: 200px" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="描述" prop="comment">
          <el-input v-model="form.comment" size="small" style="width: 200px" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="工时类型">
          <el-select v-model="form.workerType" size="small" style="width: 200px" placeholder="请选择">
            <el-option key="1" label="全职工" value="1" />
            <el-option key="2" label="小时工" value="2" />
          </el-select>
        </el-form-item>
<!--        <el-form-item label="班次类别">-->
<!--          <el-select v-model="form.scheduleType" placeholder="请选择班次类别">-->
<!--            <el-option-->
<!--              v-for="dict in scheduleTypeOptions"-->
<!--              :key="dict.dictValue"-->
<!--              :label="dict.dictLabel"-->
<!--              :value="parseInt(dict.dictValue)"-->
<!--            ></el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item label="打卡方式">
          <el-select v-model="form.clockInType" size="small" style="width: 200px" placeholder="请选择打卡方式">
            <el-option key="1" label="多次卡" value="1" />
            <el-option key="2" label="一次卡" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="第一次打卡开始时间" prop="scheduleStart1">
          <el-time-select clearable size="small" style="width: 200px"
            v-model="form.scheduleStart1"
            :picker-options="{ start: '00:00', step: '00:15', end: '23:59' }"
            type="time"
            value-format="hh:mm"
            placeholder="选择第一次打卡开始时间">
          </el-time-select>
        </el-form-item>
        <el-form-item label="第一次打卡结束时间" prop="scheduleEnd1">
          <el-time-select clearable size="small" style="width: 200px"
            v-model="form.scheduleEnd1"
                          :picker-options="{ start: '00:00', step: '00:15', end: '23:59' }"
            type="time"
            value-format="hh:mm"
            placeholder="选择第一次打卡结束时间">
          </el-time-select>
        </el-form-item>
        <el-form-item label="第二次打卡开始时间" prop="scheduleStart2">
          <el-time-select clearable size="small" style="width: 200px"
            v-model="form.scheduleStart2"
                          :picker-options="{ start: '00:00', step: '00:15', end: '23:59' }"
            type="time"
            value-format="hh:mm"
            placeholder="选择第二次打卡开始时间">
          </el-time-select>
        </el-form-item>
        <el-form-item label="第二次打卡结束时间" prop="scheduleEnd2">
          <el-time-select clearable size="small" style="width: 200px"
            v-model="form.scheduleEnd2"
                          :picker-options="{ start: '00:00', step: '00:15', end: '23:59' }"
            type="time"
            value-format="hh:mm"
            placeholder="选择第二次打卡结束时间">
          </el-time-select>
        </el-form-item>
        <el-form-item label="第三次打卡开始时间" prop="scheduleStart3">
          <el-time-select clearable size="small" style="width: 200px"
            v-model="form.scheduleStart3"
                          :picker-options="{ start: '00:00', step: '00:15', end: '23:59' }"
            type="time"
            value-format="hh:mm"
            placeholder="选择第三次打卡开始时间">
          </el-time-select>
        </el-form-item>
        <el-form-item label="第三次打卡结束时间" prop="scheduleEnd3">
          <el-time-select clearable size="small" style="width: 200px"
            v-model="form.scheduleEnd3"
                          :picker-options="{ start: '00:00', step: '00:15', end: '23:59' }"
            type="time"
            value-format="hh:mm"
            placeholder="选择第三次打卡结束时间">
          </el-time-select>
        </el-form-item>
        <el-form-item label="第四次打卡开始时间" prop="scheduleStart4">
          <el-time-select clearable size="small" style="width: 200px"
            v-model="form.scheduleStart4"
                          :picker-options="{ start: '00:00', step: '00:15', end: '23:59' }"
            type="time"
            value-format="hh:mm"
            placeholder="选择第四次打卡开始时间">
          </el-time-select>
        </el-form-item>
        <el-form-item label="第四次打卡结束时间" prop="scheduleEnd4">
          <el-time-select clearable size="small" style="width: 200px"
            v-model="form.scheduleEnd4"
                          :picker-options="{ start: '00:00', step: '00:15', end: '23:59' }"
            type="time"
            value-format="hh:mm"
            placeholder="选择第四次打卡结束时间">
          </el-time-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addSchedefine,
  delSchedefine,
  exportSchedefine,
  getSchedefine,
  listSchedefine,
  updateSchedefine
} from "@/api/business/schedefine";
import {treeselect} from "@/api/selfcom/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Schedefine",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 班次定义表格数据
      schedefineList: [],
      // 弹出层标题
      title: "",
      // 部门树选项
      deptOptions: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: undefined,
        shiftName: undefined,
        enable: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deptId: [
          { required: true, message: "组织ID不能为空", trigger: "blur" }
        ],
        shiftName: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        comment: [
          { required: true, message: "描述不能为空", trigger: "blur" }
        ],
        workerType: [
          { required: true, message: "工时类型不能为空", trigger: "blur" }
        ],
        clockInType: [
          { required: true, message: "打卡方式不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  methods: {
    /** 查询班次定义列表 */
    getList() {
      this.loading = true;
      listSchedefine(this.queryParams).then(response => {
        this.schedefineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        deptId: undefined,
        shiftName: undefined,
        comment: undefined,
        workerType: undefined,
        scheduleType: undefined,
        clockInType: undefined,
        scheduleStart1: undefined,
        scheduleEnd1: undefined,
        scheduleStart2: undefined,
        scheduleEnd2: undefined,
        scheduleStart3: undefined,
        scheduleEnd3: undefined,
        scheduleStart4: undefined,
        scheduleEnd4: undefined,
        enable: undefined,
        scheduleStart1NextDay: undefined,
        scheduleEnd1NextDay: undefined,
        scheduleStart2NextDay: undefined,
        scheduleEnd2NextDay: undefined,
        scheduleStart3NextDay: undefined,
        scheduleEnd3NextDay: undefined,
        scheduleStart4NextDay: undefined,
        scheduleEnd4NextDay: undefined
      };
      this.resetForm("form");
    },

    trueFalseFormat() {
      // , 0: 否 1: 是
      // var actions = [];
      // Object.keys(datas).map((key) => {
      //   if (datas[key].dictValue == ('' + value)) {
      //     actions.push(datas[key].dictLabel);
      //     return false;
      //   }
      // })
      // return actions.join('');
    },



    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加班次定义";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSchedefine(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改班次定义";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateSchedefine(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addSchedefine(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除班次定义编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSchedefine(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有班次定义数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSchedefine(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
